package cn.com.yiqi.wsc.portal.controller.special;

import cn.com.yiqi.wsc.common.Result;
import cn.com.yiqi.wsc.common.ResultStatusEnum;
import cn.com.yiqi.wsc.common.ResultUtil;
import cn.com.yiqi.wsc.dao.subject.criteria.SubjectCriteria;
import cn.com.yiqi.wsc.entity.subject.AdminSubjectDTO;
import cn.com.yiqi.wsc.enumeration.SubjectTypeEnum;
import cn.com.yiqi.wsc.portal.controller.BaseController;
import cn.com.yiqi.wsc.service.subject.AdminSubjectService;
import cn.com.yiqi.wsc.service.user.UserService;
import cn.com.yiqi.wsc.util.JsonUtil;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 苟宇恒 on 2017/2/22.
 * 自定义专题管理
 */
@Controller
@RequestMapping("/special")
public class SpecialController extends BaseController {

    @Autowired
    private AdminSubjectService adminSubjectService;


    @RequestMapping("/index")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView("special/index");
        this.getUserData(mv);
        return mv;
    }

    @RequestMapping("/add")
    public ModelAndView add() {
        ModelAndView mv = new ModelAndView("special/add");
        mv.addObject("detail", "false");
        mv.addObject("readonly", "false");
        this.getUserData(mv);
        return mv;
    }

    @RequestMapping("/edit")
    public ModelAndView edit(@RequestParam("specialId") Integer specialId) {
        ModelAndView mv = new ModelAndView("special/add");

        SubjectCriteria criteria = new SubjectCriteria();
        criteria.setId(specialId);
        mv.addObject("detail", JsonUtil.toJSON(adminSubjectService.detail(criteria).getData().get("detail")));
        mv.addObject("readonly", "false");
        this.getUserData(mv);
        return mv;
    }

    @RequestMapping("/detail")
    public ModelAndView detail(@RequestParam("specialId") Integer specialId) {

        ModelAndView mv = new ModelAndView("special/add");

        SubjectCriteria criteria = new SubjectCriteria();
        criteria.setId(specialId);

        mv.addObject("detail", JsonUtil.toJSON(adminSubjectService.detail(criteria).getData().get("detail")));
        mv.addObject("readonly", "true");
        this.getUserData(mv);

        return mv;
    }


    /**
     * 修改
     *
     * @param subjectDTO
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Result update(@RequestBody AdminSubjectDTO subjectDTO) {

        Result result = new Result();

        if (subjectDTO.getId() == null) {
            result.setStatus(ResultStatusEnum.参数错误.getCode());
            result.setErrMsg("参数错误");
            return result;
        }

        Result validateResult = validation(subjectDTO);

        if (!ResultUtil.isSuccess(validateResult)) {
            return validateResult;
        }

        List<String> pictures = new ArrayList<>();
        pictures.add(subjectDTO.getBannerUrl());
        subjectDTO.setBannerUrl(JsonUtil.toJSON(pictures));
        result = adminSubjectService.update(subjectDTO, userService.getCurrentUserId());

        return result;

    }

    /**
     * 删除
     *
     * @param subjectDTO
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@RequestBody AdminSubjectDTO subjectDTO) {

        Result result = new Result();


        if (subjectDTO.getId() == null) {
            result.setStatus(ResultStatusEnum.参数错误.getCode());
            result.setErrMsg("参数错误");
            return result;
        }

        result = adminSubjectService.delete(subjectDTO, userService.getCurrentUserId());

        return result;

    }


    /**
     * 删除
     *
     * @param subjectDTO
     * @return
     */
    @RequestMapping(value = "/up", method = RequestMethod.POST)
    @ResponseBody
    public Result up(@RequestBody AdminSubjectDTO subjectDTO) {

        Result result = new Result();

        if (subjectDTO.getId() == null) {
            result.setStatus(ResultStatusEnum.参数错误.getCode());
            result.setErrMsg("参数错误");
            return result;
        }

        result = adminSubjectService.changeSort(subjectDTO.getId());

        return result;

    }

    /**
     * 启用
     *
     * @param subjectDTO
     * @return
     */
    @RequestMapping(value = "/enable", method = RequestMethod.POST)
    @ResponseBody
    public Result enable(@RequestBody AdminSubjectDTO subjectDTO) {

        Result result = new Result();


        if (subjectDTO.getId() == null) {
            result.setStatus(ResultStatusEnum.参数错误.getCode());
            result.setErrMsg("参数错误");
            return result;
        }

        result = adminSubjectService.enable(subjectDTO, userService.getCurrentUserId());

        return result;

    }

    /**
     * 禁用
     *
     * @param subjectDTO
     * @return
     */
    @RequestMapping(value = "/disable", method = RequestMethod.POST)
    @ResponseBody
    public Result disable(@RequestBody AdminSubjectDTO subjectDTO) {

        Result result = new Result();


        if (subjectDTO.getId() == null) {
            result.setStatus(ResultStatusEnum.参数错误.getCode());
            result.setErrMsg("参数错误");
            return result;
        }

        result = adminSubjectService.disable(subjectDTO, userService.getCurrentUserId());


        return result;

    }

    /**
     * 列表查询
     *
     * @param criteria
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public Result pageList(@RequestBody SubjectCriteria criteria) {

        Result result = new Result();
        //criteria.setType(SubjectTypeEnum.CUSTOM.getValue());
        result = adminSubjectService.pageListCustomer(criteria);

        return result;

    }

    /**
     * 添加
     *
     * @param subjectDTO
     * @return
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ResponseBody
    public Result customInsert(@RequestBody AdminSubjectDTO subjectDTO) {
        Result result = new Result();


        Result validateResult = validation(subjectDTO);

        if (!ResultUtil.isSuccess(validateResult)) {
            return validateResult;
        }


        AdminSubjectDTO adminSubjectDTOForInsert = new AdminSubjectDTO();

        adminSubjectDTOForInsert.setName(subjectDTO.getName());
        adminSubjectDTOForInsert.setDesc(subjectDTO.getDesc());

        List<String> pictures = new ArrayList<>();
        pictures.add(subjectDTO.getBannerUrl());

        //直接拼接
        adminSubjectDTOForInsert.setBannerUrl(JsonUtil.toJSON(pictures));
        adminSubjectDTOForInsert.setType(subjectDTO.getType());

        adminSubjectDTOForInsert.setItems(subjectDTO.getItems());


        result = adminSubjectService.insert(adminSubjectDTOForInsert, userService.getCurrentUserId());

        return result;

    }

    private Result validation(AdminSubjectDTO subjectDTO) {
        Result result = new Result();

        if (StringUtils.isEmpty(subjectDTO.getName())) {
            return ResultUtil.paramError(result, "请填写名称");
        }
        if (!subjectDTO.getType().equals(3) &&  StringUtils.isEmpty(subjectDTO.getBannerUrl())) {
            return ResultUtil.paramError(result, "请上传图片");
        }

        return result;
    }

}


